import json
import socket
import numpy as np
import torch

from Knn_for_single import Knn_for_single
from SocketService import SocketService
import queue
from time import time
import numpy as np
from action_recognizition.DatabaseInteraction import DatabaseInteraction
import os

from action_recognizition.Utils import list_to_tensor

os.chdir('..')


def test_address(address, model):
    pose_list = []
    with open(address, 'r') as f:
        data = json.load(f)
    print(len(data.values()))
    for frame in data.values():
        frame[0]['keypoints'] = list_to_tensor(frame[0]['keypoints'])
        pose_list.append(frame)
        if len(pose_list) <= 68:
            continue
        else:
            del pose_list[0]
            action_predict_result = model.predict(pose_list)
            print(f'No.1: {action_predict_result[0][0]} {action_predict_result[0][1]} {action_predict_result[0][2]}')
            print(f'No.2: {action_predict_result[1][0]} {action_predict_result[1][1]} {action_predict_result[1][2]}')
            print(f'No.3: {action_predict_result[2][0]} {action_predict_result[2][1]} {action_predict_result[2][2]}')
            print('-' * 20)


if __name__ == '__main__':
    frame_list = []
    FRAME_CAPACITY = 60
    database_interaction = DatabaseInteraction()
    dataset = database_interaction.get_id_class_and_json_address()
    model = Knn_for_single(dataset)
    test_address('/home/primer/videos/action_videos/00004/action_result.json', model)

    # for i in range(100):
    #     frame = []
    #     tmp_dict = {'keypoints': torch.zeros((17, 2)), 'kp_score': torch.zeros((17, 1)), 'proposal_score': torch.zeros((1,1))}
    #     frame.append(tmp_dict)
    #     frame_list.append(frame)
    #     if len(frame_list) <= FRAME_CAPACITY:
    #         continue
    #     else:
    #         del frame_list[0]
    #         action_predict_result = model.predict(frame_list)
    #         print('No.1:', action_predict_result[0][0], action_predict_result[0][1])
    #         print('No.2:', action_predict_result[1][0], action_predict_result[1][1])
    #         print('No.3:', action_predict_result[2][0], action_predict_result[2][1])
    #         print('-' * 20)
